package cn.tedu.suppermemory.controller;

import cn.tedu.suppermemory.pojo.dto.PermissionAddNewDTO;
import cn.tedu.suppermemory.pojo.dto.PermissionUpdateDTO;
import cn.tedu.suppermemory.pojo.vo.PermissionListItemVO;
import cn.tedu.suppermemory.pojo.vo.PermissionStandardVO;
import cn.tedu.suppermemory.service.IPermissionService;
import cn.tedu.suppermemory.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 处理权限相关请求的控制器类
 *
 * @author why
 * @version 0.0.1
 */
@Api(tags = "0051. 权限管理模块")
@Slf4j
@RestController
@RequestMapping("/permissions")
@Validated
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    public PermissionController() {
        log.info("创建控制器类对象:permissionService");
    }

    // http://localhost:8888/permissions/add-new?name=.....
    @PostMapping("/add-new")
    @ApiOperation("添加权限")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid PermissionAddNewDTO permissionAddNewDTO) {
        log.info("开始处理【添加权限】的请求，参数：{}", permissionAddNewDTO);
        permissionService.addNew(permissionAddNewDTO);
        return JsonResult.ok();
    }

    // http://localhost:8888/permissions/9527/delete
    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("删除权限")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "权限ID", required = true, example = "9527", dataType = "long")
    public JsonResult<Void> delete(@Range(min = 1, max = 100000, message = "删除权限失败！权限id非法！")
            @PathVariable Long id) {
        log.info("开始处理【根据id删除权限】的请求，参数：{}", id);
        permissionService.delete(id);
        return JsonResult.ok();
    }

    // http://localhost:8888/permissions/id/update
    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改权限")
    @ApiOperationSupport(order = 300)
    public JsonResult<Void> updateInfoById(@PathVariable Long id, PermissionUpdateDTO permissionUpdateDTO) {
        log.info("开始处理【根据id修改权限】的请求，参数：{}", id);
        permissionService.updateInfoById(id, permissionUpdateDTO);
        return JsonResult.ok();
    }

    // http://localhost:8888/permissions/9527
    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据id查询权限详情")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParam(name = "id", value = "权限ID", required = true, example = "9527", dataType = "long")
    public JsonResult<PermissionStandardVO> getStandardById(@Range(min = 1, max = 1000000, message = "查询权限详情失败，权限ID非法！")
                                                       @PathVariable Long id) {
        log.debug("开始处理【根据id查询相册详情】的请求，参数：{}", id);
        PermissionStandardVO permissionStandardVO = permissionService.getStandardById(id);
        return JsonResult.ok(permissionStandardVO);
    }

    // http://localhost:8888/permissions
    @GetMapping("")
    @ApiOperation("查询权限列表")
    @ApiOperationSupport(order = 500)
    public JsonResult<List<PermissionListItemVO>> list() {
        log.info("开始处理【查询权限列表】的业务，无参数");
        List<PermissionListItemVO> list = permissionService.list();
        return JsonResult.ok(list);
    }



}
